var util = require('../../../utils/util.js');
import WeCropper from '../../we-cropper/we-cropper.js'
const device = wx.getSystemInfoSync()
const width = device.windowWidth 
const height = device.windowHeight+20
const app = getApp();
Page({
  data: {
    index: '',
    buttonClicked: false,
    cropperOpt: {
      id: 'cropper',
      width,
      height,
      scale: 2.5,
      zoom: 8,
      cut: {
        x: (width - 375) / 2,
        y: (height - 188) / 2,
        width: 375,
        height: 188
      }
    }
  },
  touchStart(e) {
    this.wecropper.touchStart(e)
  },
  touchMove(e) {
    this.wecropper.touchMove(e)
  },
  touchEnd(e) {
    this.wecropper.touchEnd(e)
  },
  getCropperImage() {
    util.buttonClicked(this);//按钮禁止重复点击
	wx.showToast({
		title: '正在处理图片',
		icon:'loading',
		duration:5000
	})
    var that = this;
    var num = wx.getStorageSync('accessToken');
    this.wecropper.getCropperImage((avatar) => {
      if (avatar) {
        wx.uploadFile({
          url: 'https://wechat.wuyuzhong.com/api/activity/upload', //仅为示例，非真实的接口地址
          filePath: avatar,
          name: 'giftImg',
          header: {
            'Content-Type': 'application/json',
			  'Accept': 'application/json',
            'Authorization': num
          },
          formData: {},
          success(response) {
            var ava = JSON.parse(response.data);
            if (that.data.index == 0) {
              app.globalData.testUrl0 = ava.data.path;
            } else if (that.data.index == 1) {
              app.globalData.testUrl1 = ava.data.path;
            } else if (that.data.index == 2) {
              app.globalData.testUrl2 = ava.data.path;
            }
            // 成功以后返回按钮状态
            that.setData({
              buttonClicked: false
            })
			  wx.hideToast();
            //  获取到裁剪后的图片
            wx.navigateBack({ //返回
              delta: 1
            })
          }
        })
      } else {
		  wx.showToast({
			  title: '获取图片失败，请稍后重试',
			  icon:'none'
		  })
      }
    })
  },
  uploadTap() {
    const self = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        const src = res.tempFilePaths[0]
        //  获取裁剪图片资源后，给data添加src属性及其值

        self.wecropper.pushOrign(src)
      }
    })
  },
  onLoad(option) {
    const {
      cropperOpt
    } = this.data

    this.setData({
      index: option.index
    })

    if (option.src) {
      cropperOpt.src = option.src
      new WeCropper(cropperOpt)
        .on('ready', (ctx) => {
        })
        .on('beforeImageLoad', (ctx) => {
          wx.showToast({
            title: '上传中',
            icon: 'loading',
            duration: 20000
          })
        })
        .on('imageLoad', (ctx) => {
          wx.hideToast()
        })
        .on('beforeDraw', (ctx, instance) => {
        })
        .updateCanvas()
    }
  }
})